/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.xmerge.adapter;

import java.util.ArrayList;
import java.util.List;

import net.xfok.rss.parser.Feed;
import net.xfok.xmerge.Christmas;
import net.xfok.xmerge.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author YangHua
 *
 */
public class FeedAdapter extends BaseAdapter {
	
	private List<Feed> feedItems;
	private LayoutInflater mInflater;
    private Bitmap itemIcon;
    private Context context;
	
	public FeedAdapter(Context context){
		mInflater = LayoutInflater.from(context);
		itemIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
		feedItems=new ArrayList<Feed>();
		this.context=context;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return feedItems.size();
	}


	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return feedItems.get(position);
	}


	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.icon = (ImageView) convertView.findViewById(R.id.item_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.text.setText(feedItems.get(position).getTitle());
		holder.text.setTextSize(Float.parseFloat(context.getResources().getText(R.string.list_feed_text_size).toString()));
		itemIcon=BitmapFactory.decodeResource(context.getResources(), Christmas.genRandomIcon());
		holder.icon.setImageBitmap(itemIcon);
		return convertView;
    }
 	static class ViewHolder {
 		TextView text;
 		ImageView icon;
 	}
 	
 	public void add(Feed feedItem){
 		if(feedItem!=null){
 			feedItems.add(feedItem);
 		}
 	}
 	
}
